package com.hystrix.command;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.hystrix.http.HttpPost;
import com.hystrix.util.Constant;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

public class CollapserBatchCommand extends HystrixCommand<List<String>> {

	List<String> ids;

	public CollapserBatchCommand(List<String> ids) {
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CollapserBatchCommand"))
				.andCommandPropertiesDefaults(
						HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(30000)));
		this.ids = ids;
	}

	@Override
	protected List<String> run() throws Exception {
		String maps = HttpPost.httpPost(Constant.serviceBbatch + "?cacheIds=" + StringUtils.join(ids, ","), "",
				"utf-8");
		JSONArray obj = JSONArray.parseArray(maps);
		List<String> res = new ArrayList<String>();
		for (int i = 0; i < obj.size(); i++) {
			res.add(obj.getString(i));
		}
		return res;
	}

}
